//
//  NSDate+RWAdditions.m
//  AudioDating
//
//  Created by ray on 2021/12/2.
//

#import "NSDate+RWAdditions.h"

@implementation NSDate (RWAdditions)

+ (NSString *)rw_timestamp {
    NSDate *date = [NSDate dateWithTimeIntervalSinceNow: 0];
    return [self rw_timestampWithDate:date];
}

+ (NSString *)rw_timestampWithDate:(NSDate *)date {
    NSTimeInterval time = [date timeIntervalSince1970];
    return [NSString stringWithFormat:@"%.0f",time];
}

+ (NSString *)rw_timestampMilli {
    NSDate *date = [NSDate dateWithTimeIntervalSinceNow: 0];
    return [self rw_timestampMilliWithDate:date];
}

+ (NSString *)rw_timestampMilliWithDate:(NSDate *)date {
    NSTimeInterval time = [date timeIntervalSince1970] * 1000;
    return [NSString stringWithFormat:@"%.0f",time];
}

+ (NSString *)rw_formatWithTimestamp:(NSString *)timestamp format:(NSString *)format {
    if (!timestamp) { return @""; }
    if (!format) { format = @"yyyy-MM-dd HH:mm:ss"; }
    NSDate *detailDate = [NSDate dateWithTimeIntervalSince1970:[timestamp doubleValue]];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; //实例化一个NSDateFormatter对象
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:format];
    return [dateFormatter stringFromDate:detailDate];
}

- (NSString *)rw_format:(NSString *)format {
    return [NSDate rw_formatWithDate:self format:format];
}

+ (NSString *)rw_formatWithDate:(NSDate *)date format:(NSString *)format {
    if (!date) { return @""; }
    if (!format) { format = @"yyyy-MM-dd HH:mm:ss"; }
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; //实例化一个NSDateFormatter对象
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:format];
    return [dateFormatter stringFromDate:date];
}

+ (NSString *)rw_dateWithISO86:(NSString *)date format:(NSString *)format {

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

//    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];

    NSDate *currentDate = [dateFormatter dateFromString:date];

    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc]init];

//    [outputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [outputFormatter setDateFormat:format];

    NSString *timeChanged = [outputFormatter stringFromDate:currentDate];
    
    return timeChanged;
}

@end
